<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <table border="1">
        <tr>
        <th>id</th>
        <th>name</th>
        <th>age</th>
        <th>sex</th>
        </tr>
    </table>
    <button>添加</button>
    <script>
        var button=document.querySelector('button');
        var data=[
            {id:1,name:'张一',age:19,sex:'男'},
            {id:2,name:'张二',age:23,sex:'男'},
            {id:3,name:'张三',age:11,sex:'女'},
            {id:4,name:'张四',age:15,sex:'男'}
        ];
        button.onclick=function(){
            // for(var i=0;i<data.length;i++){
            //     var table1=document.querySelector('table');
            //     var tr=document.createElement('tr');
            //     var td0=document.createElement('td');
            //     td0.innerHTML=data[i].id;
            //     var td1=document.createElement('td');
            //     td1.innerHTML=data[i].name;
            //     var td2=document.createElement('td');
            //     td2.innerHTML=data[i].age;
            //     var td3=document.createElement('td');
            //     td3.innerHTML=data[i].sex;
            //     tr.appendChild(td0);
            //     tr.appendChild(td1);
            //     tr.appendChild(td2);
            //     tr.appendChild(td3);              
            //     table1.appendChild(tr);
            //}
            var tabledom=document.querySelector('table');
            for(var i=0;i<data.length;i++){
                //模板字符串
                var str=`
                    <tr>
                        <td>${data[i].id}</td>
                        <td>${data[i].name}</td>
                        <td>${data[i].age}</td>
                        <td>${data[i].sex}</td>
                    </tr>
                `
                // var str='<tr><td>'+data[i].id+'</td><td>'+data[i].name+'</td><td>'+data[i].age+'</td><td>'+data[i].sex+'</td></tr>';
                tabledom.innerHTML +=str;
            }
        }
    </script>
</body>
</html>